package com.drow.aips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * 
 * 类名称：Home.java
 * <P/>
 * 类描述： 系统主界面
 * <P/>
 * 创建时间：2012-12-24
 * <P/>
 * 创建人： 史守阳
 * <P/>
 * 联系方式： ssy.vip^qq.com
 * <P/>
 * 修改人：无
 * <P/>
 * 修改时间：无
 * <P/>
 * 修改备注：无
 * <P/>
 * 版本：v1.0
 *
 */
@SuppressLint("WorldReadableFiles")
public class Home extends Activity {
	
	ImageView Ilogout;
	
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	
	int[] imageIds = new int[]
	{
		R.drawable.chat_icon , R.drawable.notes_icon
		, R.drawable.phone_icon , R.drawable.iphone_4_icon , R.drawable.address_book_icon
		, R.drawable.settings_icon, R.drawable.stocks_icon	, R.drawable.app_store_icon
	};
	
	String[] titleIds = new String[]
			{
			"发送通知" , "查看记录" 
			, "单方会议" , "多方会议" , "个人信息"
			, "修改密码" , "余额查询" , "检测更新"
			};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
		// 获取只能被本应用程序读、写的SharedPreferences对象
		preferences = getSharedPreferences("com.drow.aips", MODE_WORLD_READABLE);
		editor = preferences.edit();
		
		//删除通讯录回显数据
    	String userId = preferences.getString("userId", null);
    	
    	if (null!=preferences.getString(userId+"GID", null)&&!"".equals(preferences.getString(userId+"GID", null))) 
    	{
    		// 删除旧的用户名
    		editor.remove(userId+"GID");
    	}
    	if (null!=preferences.getString(userId+"AID", null)&&!"".equals(preferences.getString(userId+"AID", null))) 
    	{
    		// 删除旧的用户名
    		editor.remove(userId+"AID");
    	}
					
		// 提交所有存入的数据
		editor.commit();
				
		Ilogout = (ImageView) findViewById(R.id.logout);
		Ilogout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// 删除注销标识
				editor.remove("logout");
				
				// 存入注销标识
				editor.putString("logout", "true");
				// 提交所有存入的数据
				editor.commit();
				
				// 启动Main Activity
				Intent intent = new Intent(Home.this, Login.class);
				startActivity(intent);
				// 结束该Activity
				finish();
			}
		});
			
		//创建一个List对象，List对象的元素是Map
		List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < imageIds.length; i++)
		{
			Map<String, Object> listItem = new HashMap<String, Object>();
			listItem.put("image" , imageIds[i]);
			listItem.put("title" , titleIds[i]);
			listItems.add(listItem);
		}
		//创建一个SimpleAdapter
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems 
				//使用/layout/home.xml文件作为界面布局
				, R.layout.grid_item, new String[]{"image","title"}, new int[]{R.id.griditem_pic,R.id.griditem_title});
		
		GridView grid = (GridView)findViewById(R.id.home_GridView);
		//为GridView设置Adapter
		grid.setAdapter(simpleAdapter);
		
		//添加列表项被选中的监听器
		grid.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position , long id)
			{
				Intent intent = null;
				//当前被选中的图片
				switch(position % imageIds.length)
				{	
					case 0:
						// 启动Main Activity
						intent = new Intent(Home.this, Notifications.class);
						startActivity(intent);
						// 结束该Activity
						finish();
						break;
					case 1:
						// 启动Main Activity
						intent = new Intent(Home.this, Record.class);
						startActivity(intent);
						// 结束该Activity
						finish();
						break;
						
					case 2:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 3:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 4:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 5:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 6:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 7:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					default:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
				}
						
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> parent){}			
			
		});
		
		//添加列表项被单击的监听器
		grid.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				//当前被单击的图片
				switch(position % imageIds.length)
				{	
					case 0:
						// 启动Main Activity
						Intent intent = new Intent(Home.this, Notifications.class);
						startActivity(intent);
						// 结束该Activity
						finish();
						break;
					case 1:
						// 启动Main Activity
						intent = new Intent(Home.this, Record.class);
						startActivity(intent);
						// 结束该Activity
						finish();
						break;
						
					case 2:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 3:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 4:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 5:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 6:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					case 7:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
						break;
						
					default:
						Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_SHORT).show();
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_LONG).show();
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
		{
			Toast.makeText(getApplicationContext(), "当前已是主界面",  Toast.LENGTH_LONG).show();
		}else if(keyCode == KeyEvent.KEYCODE_MENU) 
		{
			Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_LONG).show();
		}
		return true;
	}

}
